package org.aom.litebean.module.security;

import java.util.List;
import java.util.Map;

import org.aom.entity.security.Authority;
import org.aom.entity.security.Resource;
import org.aom.litebean.module.base.BeforeRenderBaseBean;
import org.aom.service.security.IAuthorityManager;
import org.aom.service.security.IResourceManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;
import org.operamasks.faces.util.Browser;

@ManagedBean(name = "module.security.resourceLinkAuthoritysBean", scope = ManagedBeanScope.SESSION)
public class ResourceLinkAuthoritysBean extends BeforeRenderBaseBean {

	private Long selectedModuleId;

	private Resource selectedResource;

	public static String SELECTED_MODULE_ID = "selectedModuleId";

	@Bind(id = "uncheckedDataGrid", attribute = "binding")
	private UIDataGrid uncheckedDataGrid;

	@Bind(id = "checkedDataGrid", attribute = "binding")
	private UIDataGrid checkedDataGrid;

	@ManagedProperty("#{resourceManager}")
	private IResourceManager resourceManager;

	@ManagedProperty("#{authorityManager}")
	private IAuthorityManager authorityManager;

	@Bind(id = "uncheckedDataGrid", attribute = "dataProvider")
	public GridDataProvider getUnDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return authorityManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				List<Authority> un = authorityManager.getAll();
				List<Authority> ck = getSelectedResource().getAuthorityList();
				un.removeAll(ck);
				return un.toArray();
			}

			public int getTotalCount() {
				return authorityManager.getAll().size()
						- getSelectedResource().getAuthorityList().size();
			}
		};
	}

	@Bind(id = "checkedDataGrid", attribute = "dataProvider")
	public GridDataProvider getCkDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return authorityManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				return getSelectedResource().getAuthorityList().toArray();
			}

			public int getTotalCount() {
				return getSelectedResource().getAuthorityList().size();
			}
		};
	}

	private Resource getSelectedResource() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
		return resourceManager.get(selectedModuleId);
	}

	@Action(id = "del")
	public void del() {
		selectedResource = getSelectedResource();
		Object[] as = checkedDataGrid.getSelectedValues();
		if (as == null || as.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object authority : as) {
				selectedResource.getAuthorityList().remove(
						(Authority) authority);
			}
			resourceManager.save(selectedResource);
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.setSelections(new int[] { -1 });
			uncheckedDataGrid.reload();
			// Browser.execClientScript("window.parent.BtnJsvar.fireEvent('click');");
		}
	}

	@Action(id = "add")
	public void add() {
		selectedResource = getSelectedResource();
		Object[] rs = uncheckedDataGrid.getSelectedValues();
		if (rs == null || rs.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object authority : rs) {
				selectedResource.getAuthorityList().add((Authority) authority);
			}
			resourceManager.save(selectedResource);
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.setSelections(new int[] { -1 });
			uncheckedDataGrid.reload();
			// Browser.execClientScript("window.parent.BtnJsvar.fireEvent('click');");
		}
	}

	@BeforeRender
	protected void beforeRender(boolean isPostBack) {
		initBaseResource();
	}

}
